package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentStockRkHistory;
import com.mxpio.erp.common.equipment.service.EquipmentStockRkHistoryService;
import com.mxpio.erp.common.equipment.vo.VEquipmentStockRkHistoryDetail;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 入库记录
 * @Author: MxpIO
 * @Date:   2021-10-11
 * @Version: V1.0
 */
@Service
public class EquipmentStockRkHistoryServiceImpl extends BaseServiceImpl<EquipmentStockRkHistory> implements EquipmentStockRkHistoryService {


    @Override
    @Transactional(readOnly = true)
    public Page<EquipmentStockRkHistory> queryRkApplyHistoryByMainId(Pageable page, String mainId) {
        String sql = "select *, " +
                " (case when type_ = 0 then  '接收' else '拒绝' end) as optType " +
                " from mb_erp_equipment_stock_rk_history " +
                " where inbound_apply_id_ = '" + mainId +"'";
        List resultList = JpaUtil.nativeQuery(sql, EquipmentStockRkHistory.class).getResultList();
        String countSql = sql + " limit "  + (page.getPageNumber()+1)*page.getPageSize() + ","+page.getOffset();
        List total = JpaUtil.nativeQuery(countSql).getResultList();
        return new PageImpl<EquipmentStockRkHistory>(resultList, page, total.size());
    }

    @Override
    @Transactional(readOnly = true)
    public Page<VEquipmentStockRkHistoryDetail> queryRkApplyHistoryDetailByMainId(Pageable page, EquipmentStockRkHistory tpmStockRkHistory) {
        String sql = "SELECT\n" +
                "\t\t\trh.id,\n" +
                "\t\t\trh.current_accept,\n" +
                "\t\t\tra.stock_id,\n" +
                "\t\t\trai.materials_id,\n" +
                "\t\t\trai.batch_no,\n" +
                "\t\t\ttm.NAME AS materialsName,\n" +
                "\t\t\ttm.CODE AS materialsCode,\n" +
                "\t\t\ttm.product_name AS materialsType\n" +
                "\t\tFROM\n" +
                "\t\t\ttpm_stock_rk_history rh\n" +
                "\t\t\tLEFT JOIN tpm_stock_rk_apply ra ON ra.id = rh.inbound_apply_id\n" +
                "\t\t\tLEFT JOIN tpm_stock_rk_apply_item rai ON rai.id = rh.inboud_apply_item_id\n" +
                "\t\t\tLEFT JOIN tpm_material tm ON tm.id = rai.materials_id\n" +
                "\t\tWHERE\n" +
                "\t\t\trh.id = " + tpmStockRkHistory.getId();;
        List resultList = JpaUtil.nativeQuery(sql, VEquipmentStockRkHistoryDetail.class).getResultList();
        String countSql = sql + " limit "  + (page.getPageNumber()-1)*page.getPageSize() + ","+page.getOffset();
        List total = JpaUtil.nativeQuery(countSql).getResultList();
        return new PageImpl<VEquipmentStockRkHistoryDetail>(resultList, page, total.size());
    }
}
